package com.winchain.chainbill.tbill.biz.rx;

import com.shell.bee.base.utils.MoneyUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.SmsTBillDctAprvKeyword;
import com.shell.governor.modules.message.keyword.SmsTBillDctInsPayByBuyerKeyword;
import com.shell.governor.modules.message.keyword.SmsTBillDctRevRjctKeyword;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbOpt;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import org.springframework.stereotype.Component;

@Component
public class TBillDctRevMsgSubscriber extends OnErrorNotImplementedSubscriber<TBillEvent<TbDct>> {
	public void onApplicationEvent(TBillEvent<TbDct> t) {
		TbDct dct = (TbDct) t.getBill().getSource();
		TbRec rec = t.getBill().getRec();
		TbOpt opt = t.getOperation();
		String senderId = CommonDataUtil.getUserId();
		String dctAmt = MoneyUtil.format(dct.getDctAmt());
		String dctDate = SysDateUtil.parseDate(dct.getDctDate(), "yyyyMMdd", "yyyy-MM-dd");
		if (("E004".equals(opt.getOptCode())) || ("E003".equals(opt.getOptCode()))) {
			if (dct.getBuyerId().equals(dct.getSellerId())) {
				String template = "MT09009";
				SmsTBillDctAprvKeyword keyword = new SmsTBillDctAprvKeyword();
				keyword.setCusName(dct.getSellerNm());
				keyword.setDctAmt(dctAmt);
				keyword.setDctDate(dctDate);
				MessageSender.sendMessageByTmpl(senderId, template, keyword, dct.getFinId());
			} else {
				String template = "MT09008";
				SmsTBillDctInsPayByBuyerKeyword keyword = new SmsTBillDctInsPayByBuyerKeyword();
				keyword.setBillCode(rec.getBillCode());
				keyword.setCusName(dct.getSellerNm());
				keyword.setDctAmt(dctAmt);
				keyword.setDctInt(MoneyUtil.format(dct.getDctInt()));
				keyword.setSerFee(MoneyUtil.format(dct.getSerFee()));
				MessageSender.sendMessageByTmpl(senderId, template, keyword, dct.getBuyerId());
			}
		} else {
			String template = "MT09012";
			SmsTBillDctRevRjctKeyword keyword = new SmsTBillDctRevRjctKeyword();
			keyword.setDctAmt(dctAmt);
			keyword.setDctDate(dctDate);
			keyword.setContactorName(CommonDataUtil.getUserName());
			keyword.setRemark(opt.getRemark());
			MessageSender.sendMessageByTmpl(senderId, template, keyword, dct.getSellerId());
		}
	}
}
